<?php

App::uses('AdminController', 'Controller');

class AdminPagesController extends AdminController {
	
	public $uses = array('Page');
	
	public function index() {
		
		$pages = $this->Page->find("all", array(
			"fields" => "id, title"
		));
		
		$this->set(compact('pages'));
	}
	
	public function create() {
		$this->render("form");
	}
	
	public function edit($id) {
		$this->set('page', $this->Page->findById($id));
		$this->render("form");
	}
	
	public function save() {
		if($this->request->is('post')) {
			$data = $this->data;
			$data['Page']['key'] = urlize($data['Page']['title']);
			
			$this->Page->create();
			$this->Page->set($data);
			
			$this->set("page", $data);
			if($this->Page->validates()) {
				if($this->Page->save($data)) {
					$this->setFlash('Stranica je sačuvana.', 'ok');
					$this->redirect('/admin_pages/edit/' . $this->Page->id);
				}else{
					$this->setFlash('Stranica nije sačuvana.');
					return $this->render("form");
				}
			}else{
				$this->setFlash('Stranica nije sačuvana. Uneti podaci nisu validni.');
				return $this->render("form");
			}
		}
		
		$this->redirect("/admin_pages");
	}
	
	public function delete($id) {
		if($id>0) {
			if($this->Page->deleteAll(array('id'=>$id))) {
				$this->setFlash("Stranica je obrisana", "ok");
			}else{
				$this->setFlash("Stranica nije obrisana");
			}
		}
		$this->redirect("/admin_pages");
	}
	
}